package com.example.demo.controller;
import com.example.demo.dto.ErrorResponse;
import com.example.demo.dto.TimeDataRequest;
import com.example.demo.dto.TimeDataResponse;
import com.example.demo.exception.InvalidTimeRangeException;
import com.example.demo.service.TimeDataService;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/api/time-data")
public class TimeDataController {

    private final TimeDataService timeDataService;

    public TimeDataController(TimeDataService timeDataService) {
        this.timeDataService = timeDataService;
    }

    @PostMapping
    public ResponseEntity<?> getTimeData(@RequestBody TimeDataRequest request) {
        try {
            validateRequest(request.getStartTime(), request.getEndTime());
            List<TimeDataResponse> response = timeDataService.generateTimeData(
                    request.getStartTime(), request.getEndTime());
            return ResponseEntity.status(HttpStatus.OK).body(new ErrorResponse(HttpStatus.OK.value(), "调存数据成功"));
        } catch (InvalidTimeRangeException e) {
            return ResponseEntity.badRequest().body(
                    new ErrorResponse(HttpStatus.BAD_REQUEST.value(), e.getMessage()));
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(
                    new ErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR.value(), "内部服务器错误"));
        }
    }

    @GetMapping
    public ResponseEntity<?> getTimeDataByParams(
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime startTime,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime endTime) {
        try {
            validateRequest(startTime, endTime);
            List<TimeDataResponse> response = timeDataService.generateTimeData(startTime, endTime);
            return ResponseEntity.ok(response);
        } catch (InvalidTimeRangeException e) {
            return ResponseEntity.badRequest().body(
                    new ErrorResponse(HttpStatus.BAD_REQUEST.value(), e.getMessage()));
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().body(
                    new ErrorResponse(HttpStatus.BAD_REQUEST.value(), e.getMessage()));
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(
                    new ErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR.value(), "内部服务器错误"));
        }
    }

    private void validateRequest(LocalDateTime startTime, LocalDateTime endTime) {
        if (startTime == null || endTime == null) {
            throw new IllegalArgumentException("开始时间和结束时间不能为空");
        }
        if (startTime.isAfter(endTime)) {
            throw new InvalidTimeRangeException("开始时间不能晚于结束时间");
        }
        if (startTime.isAfter(LocalDateTime.now())) {
            throw new InvalidTimeRangeException("开始时间不能晚于当前时间");
        }
    }
}